/*************************************************************************
    > File Name: client.cpp
    > Author: Lancer
    > Mail: 18826075768@163.com
************************************************************************/

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#define INPUT 0
#define OUTPUT 1

using namespace std;

int main(){
    int fd[2];
    pid_t pid;
    char buf[256];
    int return_count;

    pipe(fd);

    pid = fork();
    if(pid < 0){
        printf("Error in fork\n");
        exit(1);
    }
    else if(pid == 0){
        printf("in the child process...\n");
        close(fd[INPUT]);
        write(fd[OUTPUT], "hello world!\n", strlen("hello world!\n"));
        exit(0);
    }
    else{
        printf("in parent process...\n");
        close(fd[OUTPUT]);
        return_count = read(fd[INPUT], buf, sizeof(buf));
        printf("%d bytes of data received from child process: %s\n", return_count, buf);
    }

    return 0;
}